package com.dfbz.service.impl;


import com.dfbz.dao.UserDao;
import com.dfbz.model.PageEntity;
import com.dfbz.model.User;
import com.dfbz.service.UserServer;

import java.util.List;

public class UserServerImpl implements UserServer {

    UserDao userDao= new UserDao(User.class);

    @Override
    public boolean toRegister(User user) {
        boolean b = userDao.toRegister(user);
        return b;
    }

    @Override
    public PageEntity<User> findUser(Integer currPage, String username) {
        PageEntity<User> entity = new PageEntity<>();
        entity.setCurrPage(currPage);
        //页大小
        Integer pageSize = entity.getPageSize();
        //计算起始索引
        Integer startIndex=(currPage-1)*pageSize;
        //当前页数据
        List<User> list = userDao.findUser(startIndex,pageSize,username);
        //总记录数
        Integer totalCount = userDao.findAll();
        //总页数
        Integer totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;
        entity.setCurrPage(currPage);
        entity.setData(list);
        entity.setTotalCount(totalCount);
        entity.setTotalPage(totalPage);
        return entity;
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }


    @Override
    public List<User> toLogin(String username, String password) {
        return userDao.toLogin(username, password);
    }

    @Override
    public User findById(Integer id) {
        User user = userDao.getById(id);
        return user;
    }
}
